package com.andatsoft.laisim.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.andatsoft.laisim.R;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;

public class ToastMessage {

	private Toast mToast;
	private Context mContext;
	FrameLayout mLayout;

	public ToastMessage(Context c) {
		mToast = new Toast(c);
		mContext = c;
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	private View getView(String content) {
		View v = ((LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.toast_view, null);
		FrameLayout.LayoutParams pr = new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		v.setLayoutParams(pr);
		TextView tv = (TextView) v.findViewById(R.id.tv_toast_content);
		Theme theme = ThemeManager.getSelectedTheme();
		tv.setTextColor(theme.getToastMessageColor());
		tv.setTextSize(theme.getToastMessageSize());
		tv.setText(content);
		if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD_TO_ICS) {
			tv.setBackground(createBackground(theme
					.getDialogBackgroundColor()));
		} else {
			tv.setBackgroundDrawable(createBackground(theme
					.getDialogBackgroundColor()));
		}

		return v;
	}

	private ShapeDrawable createBackground(int color) {
		ShapeDrawable sd = new ShapeDrawable();
		float radius = 50f;
		float[] radi = new float[] { radius, radius, radius, radius, radius,
				radius, radius, radius };
		Shape s = new RoundRectShape(radi, new RectF(), radi);
		sd.setShape(s);
		sd.getPaint().setColor(color);
		return sd;
	}

	public void showShortMessage(String content) {
		mToast.setView(getView(content));
		mToast.setDuration(Toast.LENGTH_SHORT);
		mToast.show();
	}

	public void showLongMessage(String content) {
		mToast.setView(getView(content));
		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.show();
	}
}
